package gui.passwordView;

import gui.commons.Resources;
import gui.mainView.UIView;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRootPane;

public class UIPasswordView extends JInternalFrame{

	private static final long serialVersionUID = 1L;
	private static UIPasswordView INSTANCE = null;
	private UIPasswdControler controler;
	private UIView parent;

	private JPanel adminPan = new JPanel();
	private JPanel guestPan = new JPanel();
	private JLabel title = new JLabel("Settings");
	private JLabel info = new JLabel("Passwords must contain 4 digits. e.g. 1234");
	private JLabel adminTxt = new JLabel("Edit admin password");
	private JLabel guestTxt = new JLabel("Edit guest password");
	public IntegerField currentAdminPasswd;
	public IntegerField currentGuestPasswd;
	public IntegerField newAdminPasswd;
	public IntegerField newGuestPasswd;

	public JButton submit = new JButton("Apply changes");
	public JButton close;
	public JLabel exceptionAdmin= new JLabel();
	public JLabel exceptionGuest= new JLabel();

	private int x,y,a,b;
	private GridBagConstraints constraintTitle;
	private GridBagConstraints constraintInfo;
	private GridBagConstraints constraintAdmin;
	private GridBagConstraints constraintGuest;
	private GridBagConstraints constraintSubmit;
	private GridBagConstraints constraintException;
	private GridBagConstraints constraintClose;

	private JPanel contentPane;

	public UIPasswordView(UIView parent){
		this.contentPane=new JPanel();
		this.parent=parent;
		this.controler = new UIPasswdControler(this, this.parent);

		Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		this.y = (int)dim.getHeight();
		this.x = (int)dim.getWidth();
		this.a = (int)(dim.width*0.275);
		this.b= (int)(dim.height*0.10);

		this.currentAdminPasswd = new IntegerField();
		this.currentGuestPasswd = new IntegerField();
		this.newAdminPasswd = new IntegerField();
		this.newGuestPasswd = new IntegerField();
			
		this.contentPane.setOpaque(true);
		this.contentPane.setBackground(Color.BLACK);

		this.setContentPane(contentPane);
		//LAYOUT
		this.contentPane.setLayout(new GridBagLayout());

		//Fonts
		Font titleFont = new Font("Arial", Font.BOLD, (int)(x/50));
		Font infoFont = new Font("Arial", Font.ITALIC, (int)(x/100));
		Font info2Font = new Font("Arial", Font.BOLD, (int)(x/75));

		//Cellule Title
		Insets i = new Insets(0, 0, 10, 0); // Marges Haut gauche Bas Droite
		this.constraintTitle = this.createConstraint(0,0,2,1,GridBagConstraints.PAGE_START,GridBagConstraints.NONE,i);

		this.title.setFont(titleFont); 
		this.title.setForeground(Color.white);
		this.contentPane.add(this.title,this.constraintTitle);

		//Cellule Info
		i = new Insets(0, 0, 10, 0); // Haut gauche Bas Droite
		this.constraintInfo = this.createConstraint(0,1,2,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);

		this.info.setFont(infoFont); 
		this.info.setForeground(Color.white);
		this.contentPane.add(this.info,this.constraintInfo);

		//Cellule AdminPasswd
		i = new Insets(0, 0, 0, 0); // Haut gauche Bas Droite
		this.constraintAdmin = this.createConstraint(0,2,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);

		this.adminPan.setLayout(new GridBagLayout());
		this.adminTxt.setFont(info2Font);
		this.adminTxt.setForeground(Color.white);
		GridBagConstraints gbcAdmin = new GridBagConstraints();
		i = new Insets(10, 10, 0, 10); // Haut gauche Bas Droite
		gbcAdmin = this.createConstraint(0,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.adminPan.add(adminTxt,gbcAdmin);

		currentAdminPasswd.setPreferredSize(new Dimension(150, 25));
		gbcAdmin = this.createConstraint(0,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.adminPan.add(currentAdminPasswd,gbcAdmin);

		newAdminPasswd.setPreferredSize(new Dimension(150, 25));
		gbcAdmin = this.createConstraint(0,2,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.adminPan.add(newAdminPasswd,gbcAdmin);

		this.adminPan.setOpaque(false);
		this.contentPane.add(this.adminPan,this.constraintAdmin);

		//Cellule currentGuestPasswd
		i = new Insets(0, 0, 0, 0); // Haut gauche Bas Droite
		this.constraintGuest = this.createConstraint(1,2,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);

		this.guestPan.setLayout(new GridBagLayout());

		this.guestTxt.setFont(info2Font);
		this.guestTxt.setForeground(Color.white);
		GridBagConstraints gbcGuest = new GridBagConstraints();
		i = new Insets(10, 10, 0, 10); // Haut gauche Bas Droite
		gbcGuest = this.createConstraint(0,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.guestPan.add(guestTxt,gbcGuest);

		currentGuestPasswd.setPreferredSize(new Dimension(150, 25));
		gbcGuest = this.createConstraint(0,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.guestPan.add(currentGuestPasswd,gbcGuest);

		newGuestPasswd.setPreferredSize(new Dimension(150, 25));
		gbcGuest = this.createConstraint(0,2,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.guestPan.add(newGuestPasswd,gbcGuest);

		this.guestPan.setOpaque(false);
		this.contentPane.add(this.guestPan,this.constraintGuest);

		//Cellule submit
		i = new Insets(20, 0, 0, 0); // Haut gauche Bas Droite
		this.constraintSubmit = this.createConstraint(0,4,2,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);

		submit.addActionListener(controler);
		this.contentPane.add(this.submit,this.constraintSubmit);

		//Cellule Fermer
		i = new Insets(0, 0, 10, 0); // Haut gauche Bas Droite
		this.constraintClose = this.createConstraint(1,0,1,1,GridBagConstraints.FIRST_LINE_END,GridBagConstraints.NONE,i);

		this.close = new JButton(new ImageIcon(Resources.delIcon));
		this.close.setBorder(null);
		this.close.setBackground(null);
		this.close.addActionListener(controler);
		this.add(this.close,this.constraintClose);

		this.setTitle("AirPi Settings");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize((int)(x*0.40),(int)(y*0.40));
		this.setLocation(a, b);
		this.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE);
		this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
		this.setBorder(null);
		this.setResizable(false);
		this.setVisible(true);
	}

	public void update(){		
		Font exceptionFont = new Font("Arial", Font.BOLD, (int)(x/125));				
		this.contentPane.removeAll();
		this.contentPane.setLayout(new GridBagLayout());

		this.contentPane.add(this.title,this.constraintTitle);
		this.contentPane.add(this.close,this.constraintClose);
		this.contentPane.add(this.info,this.constraintInfo);
		this.contentPane.add(this.adminPan,this.constraintAdmin);
		this.contentPane.add(this.guestPan,this.constraintGuest);

		//Cellule exceptionAdmin
		Insets i = new Insets(20, 0, 0, 0);
		this.constraintException = this.createConstraint(0,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.exceptionAdmin.setFont(exceptionFont);
		this.contentPane.add(this.exceptionAdmin,this.constraintException);

		//Cellule exceptionGuest
		this.constraintException = this.createConstraint(1,3,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.exceptionGuest.setFont(exceptionFont);
		this.contentPane.add(this.exceptionGuest,this.constraintException);
		this.contentPane.add(this.submit,this.constraintSubmit);

		this.contentPane.validate();
		this.repaint();

	}

	public static synchronized UIPasswordView getInstance(UIView parent)
	{	
		if (INSTANCE == null){ 
			INSTANCE = new UIPasswordView(parent);
		}
		return INSTANCE;
	}

	public GridBagConstraints createConstraint(int gx, int gy, int gw, int gh, int anchor, int fill, Insets insets){
		GridBagConstraints gbc=new GridBagConstraints();
		gbc.gridx=gx;	//colonne
		gbc.gridy=gy;	//ligne
		gbc.gridwidth = gw;	//nb de colonnes occupées
		gbc.gridheight = gh;	//nb de lignes occupées
		gbc.anchor=anchor;	//position du composant dans la cellule
		gbc.fill = fill;	//redimensionnement du composant
		gbc.insets = insets;	//marges autour du composant 
		return gbc;
	}

	public synchronized void kill(){
		parent.settingsOpened=false;
		this.dispose();
		this.INSTANCE=null;
	}
}
